<template>
	<view :class="['v-radio-group', vertical ? 'vertical' : 'horizontal']" :style="groupStyle" role="radiogroup">
		<slot />
	</view>
</template>

<script lang="ts" setup>
import { ref, provide, computed, watch } from 'vue';

const props = defineProps({
	value: {
		type: [String, Number],
		default: ''
	},
	vertical: {
		type: Boolean,
		default: false
	},
	disabled: {
		type: Boolean,
		default: false
	},
	groupStyle: {
		type: Object,
		default: () => ({})
	},
	defaultValue: {
		type: [String, Number],
		default: ''
	}
});

const emit = defineEmits(['update:value', 'change']);
const currentValue = ref(props.value);

watch(
	() => props.value,
	(newVal) => {
		currentValue.value = newVal;
	}
);

provide('radioGroup', {
	value: currentValue,
	disabled: computed(() => props.disabled),
	defaultValue: computed(() => props.defaultValue), // 提供默认值
	emitUpdate: (newValue: any) => {
		currentValue.value = newValue;
		emit('update:value', newValue);
		emit('change', newValue);
	}
});

watch(currentValue, (newVal) => {
	emit('update:value', newVal);
	emit('change', newVal);
});
</script>

<style lang="scss">
.v-radio-group {
	height: 72rpx;
	&.horizontal {
		display: flex;
		flex-wrap: wrap;

		.v-radio {
			margin-right: 32rpx;

			&:last-child {
				margin-right: 0;
			}
		}
	}

	&.vertical {
		.v-radio {
			margin-bottom: 24rpx;

			&:last-child {
				margin-bottom: 0;
			}
		}
	}
}
</style>
